﻿using BookManege.entity;
using BookManege.service;

namespace BookManege.controller
{
    public partial class index : Form
    {
        private BookManegeService bookManegeService;
        private bookBorrowService bookBorrowService;
        private BorrowManegeService borrowManegeService;
        private UserManegeService userManegeService;
        private StillApplication StillApplication;
        public index()
        {
            InitializeComponent();
            bookBorrowService = new bookBorrowService();
            bookManegeService = new BookManegeService();
            borrowManegeService = new BorrowManegeService();
            userManegeService = new UserManegeService();
            StillApplication = new StillApplication();
            label4.Text = bookManegeService.GetCount().ToString();
            //如果是普通用户就去除信息面板
            if (Program.user.Authority != 1)
            {
                tabControl1.TabPages.Remove(tabPage2);
            }
            else
            {
                dataGridView2.Visible = false;
            }
            
        }

        private void index_Load(object sender, EventArgs e)
        {
            /*
             * 管理员面板
             */
            //右上
            label6.Text = (((float)borrowManegeService.GetBorrowBookCount() / (float)bookManegeService.GetCount()) * 100).ToString("0.00");
            label10.Text = borrowManegeService.GetBorrowLastDateBookName().ToString();
            label11.Text = borrowManegeService.GetBorrowMostBookName().ToString();
            //右下
            label13.Text = ((float)(borrowManegeService.GetStillCount() / (float)borrowManegeService.GetCount()) * 100).ToString("0.00");
            label14.Text = borrowManegeService.BorrowTodayCount().ToString();
            label16.Text = borrowManegeService.StillTodatCount().ToString();
            //左上
            label19.Text = userManegeService.GetCount().ToString();
            label22.Text = userManegeService.GetExceedDateCount().ToString();
            label24.Text = borrowManegeService.GetCount().ToString();
            //左下
            dataGridView1.DataSource = borrowManegeService.GetExceedList();
            /*
             * 个人信息
             */
            pictureBox4.ImageLocation = Program.user.Avatar;
            textBox2.Text = Program.user.UserName;
            textBox1.Text = Program.user.Id.ToString();
            textBox3.Text = Program.user.NickName;
            textBox4.Text = Program.user.Age.ToString();
            if (Program.user.Sex == 0)
            {
                radioButton1.Checked = true;
            }
            else
            {
                radioButton2.Checked = true;
            }
            textBox5.Text = Program.user.Phone.ToString();
            textBox6.Text = Program.user.Email;
            //申请信息
            GetStillApplication();
        }
        private void GetStillApplication()
        {
            dataGridView2.DataSource = StillApplication.GetUserStillApplication();
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                DataGridViewRow row = dataGridView2.Rows[i];
                string state = row.Cells["处理情况"].Value.ToString();
                string result = row.Cells["处理结果"].Value.ToString();

                if (!string.IsNullOrEmpty(state))
                {
                    if(state == "0")
                    {
                        dataGridView2.Invoke((MethodInvoker)delegate { row.Cells["处理情况"].Value = "未处理"; });
                    }
                    else
                    {
                        dataGridView2.Invoke((MethodInvoker)delegate { row.Cells["处理情况"].Value = "已处理"; });
                    }
                    
                }
                if(!string.IsNullOrEmpty(result))
                {
                    if (state == "0")
                    {
                        dataGridView2.Invoke((MethodInvoker)delegate { row.Cells["处理结果"].Value = "驳回"; });
                    }
                    else
                    {
                        dataGridView2.Invoke((MethodInvoker)delegate { row.Cells["处理结果"].Value = "同意"; });
                    }
                }
                    
            }
               
        }
        private void button1_Click(object sender, EventArgs e)
        {
            button2.Visible = true;
            button1.Visible = false;
            textBox1.ReadOnly = textBox2.ReadOnly = textBox3.ReadOnly = textBox4.ReadOnly = textBox5.ReadOnly = textBox6.ReadOnly = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Visible = true;
            button2.Visible = false;
            textBox1.ReadOnly = textBox2.ReadOnly = textBox3.ReadOnly = textBox4.ReadOnly = textBox5.ReadOnly = textBox6.ReadOnly = true;
            try
            {
                User user = new()
                {
                    Id = Convert.ToInt64(textBox1.Text.Trim()),
                    NickName = textBox3.Text.Trim(),
                    Age = Convert.ToInt32(textBox4.Text),
                    Sex = radioButton1.Checked ? 0 : 1,
                    Phone = Convert.ToInt32(textBox5.Text.Trim()),
                    Email = textBox6.Text.Trim(),
                    UserName = textBox2.Text.Trim()
                };
                Program.user = user;
            }catch(Exception ex)
            {
                MessageBox.Show("输入格式不正确！"+ ex);
            }
            MessageBox.Show(userManegeService.UpdateUser(Program.user)>0?"成功":"失败");
        }
    }
}
